package com.longtop.leaning.dao;

import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class RMSUtil {

	public static final String FLT_RMS_NAME = "flt";
	public static final String USER_RMS_NAME = "user";
	
	public static RecordStore getUserRecordStore() {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore(USER_RMS_NAME, true);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public static RecordStore getFltRecordStore() {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore(FLT_RMS_NAME, true);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public static void closeRecordStore(RecordStore rs) {
		try {
			rs.closeRecordStore();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}

	public static int addRecord(RecordStore rs, byte[] bytes) {
		System.out.println("add record bytes : " + bytes);
		try {
			return rs.addRecord(bytes, 0, bytes.length);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static RecordEnumeration enumerateRecords(RecordStore rs, RecordFilter fltFilter,
			RecordComparator fltRecordComparator, boolean b) {
		try {
			return rs.enumerateRecords(fltFilter, fltRecordComparator, b);
		} catch (RecordStoreNotOpenException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static byte[] nextRecord(RecordEnumeration re) {
		try {
			return re.nextRecord();
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void deleteRecordStore(String recordStoreName) {
		try {
			RecordStore.deleteRecordStore(recordStoreName);
		} catch (RecordStoreException e) {
			e.printStackTrace();
		}
	}
}
